<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>找到最接近0的数字</title>
</head>
<body>
  <script>
    function fn(nums) {
      let min = Infinity, sum = 0;
      for (let num of nums) {
        let res = Math.abs(num)
          if (res < min) {
            min = res
            sum = num
          }
      }
      return sum
    }
    console.log(fn([-4, -2, 1, 4, 8]))
  </script>
</body>
</html>